home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / Jim's CDEFs v1.30 / demo Source ƒ / demoShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-06  |  14.8 KB  |  616 lines  |  [TEXT/KAHL]

  1. // -----------------------------------------------------------------------------
  2. //    File        : demoShell.c
  3. //    Date        : October 14, 1994
  4. //    Author        : somebody, modified by Jim Stout
  5. //    Purpose        : A simple shell program to drive demos of "jimsCDEFs" in
  6. //                : both a normal window and a MovableModal dialog.
  7. //                :
  8. //                : see demoDialog.c for the demoCDEF Dialog and,
  9. //                :      demoWind.c for routines used for the demoCDEF Window.
  10. //
  11. // -----------------------------------------------------------------------------
  12.  
  13. #include <CommResources.h>            // Only needed for System 6 < MacII ROMs
  14. #include <CTBUtilities.h>            // ditto
  15.  
  16. #include <GestaltEqu.h>                // Toolbox 
  17. #include <Traps.h>
  18.  
  19. #include "demoShell.h"                // demo routines
  20. #include "demoDialog.h"
  21. #include "demoTab.h"
  22. #include "demoWind.h"
  23.  
  24. #include "dialogAssist.h"            // utility routines
  25. #include "movableModal.h"
  26.  
  27. #include "jimsCDEF.h"                // cdef stuff
  28.  
  29. #define GROWICON false
  30.  
  31. //=============================================================================
  32. // demoShell globals
  33. //=============================================================================
  34.  
  35. SysEnvRec        gSysEnv;
  36. Boolean            gQuit;
  37. Boolean            gInBackground;
  38.  
  39. short            gDemoType;    
  40. ControlHandle    gPopCtl, gBoxCtl;
  41. ControlHandle    gProgBar1, gProgBar2, gProgBar3;
  42.             
  43. //=============================================================================
  44. // demo entry point
  45. //=============================================================================
  46.  
  47. main()
  48. {
  49.     MaxApplZone();
  50.     initToolbox();
  51.     getEvents();
  52. }
  53.  
  54. //=============================================================================
  55. //    initialize Mac Toolbox & check for right environment
  56. //=============================================================================
  57. void initToolbox()
  58. {
  59.     Handle        menuBar;
  60.     EventRecord thisEvent;
  61.     short        count;
  62.  
  63.     gInBackground = FALSE;
  64.     gQuit = FALSE;
  65.  
  66.     InitGraf((Ptr) &qd.thePort);
  67.     InitFonts();
  68.     InitWindows();
  69.     InitMenus();
  70.     TEInit();
  71.     InitDialogs(NIL);
  72.     InitCursor();
  73.     
  74.     if(daGestalt(gestaltCTBVersion) >= 0x0100) {
  75.         InitCRM();                        // Only needed for System 6 < MacII ROMs
  76.         InitCTBUtilities();                // ditto
  77.     }
  78.         
  79.     for (count = 1; count <= 3; ++count)
  80.         EventAvail(everyEvent, &thisEvent);
  81.         
  82.     SysEnvirons(curSysEnvVers, &gSysEnv);
  83.  
  84.     if (gSysEnv.machineType < 0)
  85.         doAlert(errNeedMacPlus, true);
  86.  
  87.     if (gSysEnv.systemVersion < 0x0600)
  88.         doAlert(errNeedSys6, true);
  89.  
  90.     if (!trapAvailable(_WaitNextEvent))
  91.         doAlert(errNeedMultiF, true);
  92.  
  93.     menuBar = GetNewMBar(rMenuBar);
  94.     if ( menuBar == NIL )
  95.          doAlert(errBadRsrc, true);
  96.          
  97.     SetMenuBar(menuBar);
  98.     DisposHandle(menuBar);
  99.     
  100.     AddResMenu(GetMHandle(mApple), 'DRVR');    // Add DA names to Apple menu
  101.     
  102.     doNewWindow();                            // create demo Window
  103.     adjustMenus();
  104.     DrawMenuBar();
  105. }
  106.  
  107. //=============================================================================
  108. // our main event loop.  We drive our Progress bars from here.
  109. //=============================================================================
  110. void getEvents()
  111. {
  112.     RgnHandle    cursorRgn;
  113.     Boolean        gotEvent;
  114.     EventRecord    thisEvent;
  115.     Point        mouse;
  116.     long        sleep;
  117.     
  118.     if(gSysEnv.systemVersion < 0x0700)
  119.         sleep = 1L;                            // so progress bars work
  120.     else
  121.         sleep = MAXLONG;
  122.  
  123.     cursorRgn = NIL;
  124.     while ( !gQuit ) {
  125.         gotEvent = WaitNextEvent(everyEvent, &thisEvent, sleep, cursorRgn);
  126.         if ( gotEvent ) {
  127.             doProcessEvents(&thisEvent);
  128.         }
  129.         doPeriodicEvent(&thisEvent);
  130.     }
  131. }
  132.  
  133. //=============================================================================
  134. //    process our events
  135. //=============================================================================
  136. void doProcessEvents(EventRecord *thisEvent)
  137. {
  138.     switch ( thisEvent->what ) {
  139.         case mouseDown:
  140.             doMouseDown(thisEvent);
  141.         break;
  142.         
  143.         case keyDown:
  144.         case autoKey:
  145.             doKeyPress(thisEvent);
  146.         break;
  147.         
  148.         case activateEvt:
  149.             doActivate(thisEvent);
  150.             break;
  151.             
  152.         case updateEvt:
  153.             doUpdate(thisEvent);
  154.         break;
  155.             
  156.         case diskEvt:
  157.             diskEvent(thisEvent);            // in movableModal
  158.         break;
  159.             
  160.         case osEvt:
  161.             doOSEvent(thisEvent);
  162.         break;
  163.     }
  164. }
  165.  
  166. void doPeriodicEvent ( EventRecord * thisEvent )
  167. {
  168.     if(isAppWindow(FrontWindow())) {
  169.         if(gDemoType == 6)
  170.             doProgress();
  171.     }
  172. }
  173.  
  174. //=============================================================================
  175. //    activate our window
  176. //=============================================================================
  177. void doActivate(EventRecord *thisEvent)
  178. {
  179.     WindowPtr    theWindow;
  180.     Boolean        becomingActive;
  181.  
  182.     theWindow = (WindowPtr) thisEvent->message;
  183.     becomingActive = (thisEvent->modifiers & activeFlag) != 0;
  184. #if GROWICON
  185.     if ( isAppWindow(theWindow) ) {
  186.         DrawGrowIcon(theWindow);
  187.     }
  188. #endif
  189. }
  190.  
  191. //=============================================================================
  192. //    doKeyPress
  193. //=============================================================================
  194. void doKeyPress(EventRecord *thisEvent)
  195. {
  196.     char    key;
  197.  
  198.     key = thisEvent->message & charCodeMask;
  199.     if ( thisEvent->modifiers & cmdKey ) {
  200.         adjustMenus();
  201.         doMenuCommand(MenuKey(key));
  202.     } else {
  203. //        doKeyPress(thisEvent);
  204.     }
  205. }
  206.  
  207. //=============================================================================
  208. //    react to a MouseDown in our window
  209. //=============================================================================
  210. void doMouseDown(EventRecord *thisEvent)
  211. {
  212.     long        newSize;
  213.     Rect        growRect;
  214.     WindowPtr    theWindow;
  215.     short        part = FindWindow(thisEvent->where, &theWindow);
  216.  
  217.     switch ( part ) {
  218.         case inMenuBar:
  219.             adjustMenus();
  220.             doMenuCommand(MenuSelect(thisEvent->where));
  221.         break;
  222.             
  223.         case inSysWindow:
  224.             SystemClick(thisEvent, theWindow);
  225.         break;
  226.         
  227.         case inContent:
  228.             if ( theWindow != FrontWindow() )
  229.                 SelectWindow(theWindow);
  230.             else
  231.                 doContentClick(thisEvent, theWindow);
  232.         break;
  233.         
  234.         case inDrag:                // Pass screenBits.bounds to get all gDevices
  235.             DragWindow(theWindow, thisEvent->where, &qd.screenBits.bounds);
  236.         break;
  237.         
  238.         case inGrow:
  239.             growRect = qd.screenBits.bounds;
  240.             growRect.top = growRect.left = 80;        // Arbitrary minimum size.
  241.             newSize = GrowWindow(theWindow, thisEvent->where, &growRect);
  242.             if (newSize != 0) {
  243.                 invalidateScrollbars(theWindow);
  244.                 SizeWindow(theWindow, LOWORD(newSize), HIWORD(newSize), TRUE);
  245.                 invalidateScrollbars(theWindow);
  246.             }
  247.         break;
  248.         
  249.         case inGoAway:
  250.             if (TrackGoAway(theWindow, thisEvent->where))  {
  251.                 closeAnyWindow(theWindow);
  252.             }
  253.         break;
  254.         
  255.         case inZoomIn:
  256.         case inZoomOut:
  257.             if (TrackBox(theWindow, thisEvent->where, part)) {
  258.                 SetPort(theWindow);
  259.                 EraseRect(&theWindow->portRect);
  260.                 InvalRect(&theWindow->portRect);
  261.             }
  262.         break;
  263.     }
  264. }
  265.  
  266. //=============================================================================
  267. //    doContent Click
  268. //=============================================================================
  269. void doContentClick ( EventRecord *thisEvent, WindowPtr theWindow)
  270. {
  271.     Point            mousePt;
  272.     short            thePart;
  273.     ControlHandle    theCtl;
  274.     
  275.     mousePt = thisEvent->where;
  276.     GlobalToLocal(&mousePt);
  277.     thePart = FindControl(mousePt, theWindow, &theCtl);
  278.     
  279.     if(thePart && (theCtl == gPopCtl || gDemoType == 2)) {
  280. #ifdef NEW_HEADERS_AVAILABLE
  281.         TrackControl(theCtl, mousePt,  (ControlActionUPP)-1);
  282. #else
  283.         TrackControl(theCtl, mousePt,  (ProcPtr)-1);
  284. #endif
  285.         if(theCtl == gPopCtl)
  286.             doSwapControls(theWindow);
  287.     }
  288.     else
  289.     if(thePart && theCtl) {
  290.         thePart = TrackControl(theCtl, mousePt, nil);
  291.         if(thePart)
  292.             doControlClick(theCtl, thePart);
  293.     }
  294.     
  295. }
  296. //=============================================================================
  297. //    doOSEvent
  298. //=============================================================================
  299. void doOSEvent(EventRecord *thisEvent)
  300. {
  301.     switch ((thisEvent->message >> 24) & 0x00FF) {        // High byte of message
  302.         case suspendResumeMessage:
  303.             gInBackground = (thisEvent->message & resumeFlag) == 0;
  304. #if GROWICON
  305.             if (FrontWindow()) {
  306.                 DrawGrowIcon(FrontWindow());
  307.             }
  308. #endif
  309.         break;
  310.         
  311.         case mouseMovedMessage:
  312.             break;
  313.     }
  314. }
  315.  
  316. //=============================================================================
  317. //    doUpdate
  318. //=============================================================================
  319. void doUpdate(EventRecord * thisEvent)
  320. {
  321.     WindowPtr theWindow = (WindowPtr)thisEvent->message;
  322.     
  323.     if ( isAppWindow(theWindow) ) {
  324.         BeginUpdate(theWindow);
  325.         if (!EmptyRgn(theWindow->visRgn)) {
  326.             SetPort(theWindow);
  327.             EraseRgn(theWindow->visRgn);
  328.             DrawControls(theWindow);
  329. #if GROWICON
  330.             DrawGrowIcon(theWindow);
  331. #endif
  332.         }
  333.         EndUpdate(theWindow);
  334.     }
  335. }
  336.  
  337. //=============================================================================
  338. //    adjustMenus
  339. //=============================================================================
  340. void adjustMenus()
  341. {
  342.     WindowPtr    window;
  343.     MenuHandle    menu;
  344.  
  345.     window = FrontWindow();
  346.  
  347.     menu = GetMHandle(mFile);
  348.     if ( window != NIL ) {
  349.         EnableItem(menu, iClose);
  350.     }
  351.     else {
  352.         DisableItem(menu, iClose);
  353.     }
  354.     menu = GetMHandle(mEdit);
  355.     if ( isDAWindow(window) ) {        // A desk accessory might need the edit menu…
  356.         EnableItem(menu, iUndo);
  357.         EnableItem(menu, iCut);
  358.         EnableItem(menu, iCopy);
  359.         EnableItem(menu, iClear);
  360.         EnableItem(menu, iPaste);
  361.     } else {                        // … but we don’t use it.
  362.         DisableItem(menu, iUndo);
  363.         DisableItem(menu, iCut);
  364.         DisableItem(menu, iCopy);
  365.         DisableItem(menu, iClear);
  366.         DisableItem(menu, iPaste);
  367.     }                
  368.     menu = GetMHandle(mDemo);
  369.     if ( window != NIL ) {
  370.         DisableItem(menu, iDemoWind);
  371.     }
  372.     else {
  373.         EnableItem(menu, iDemoWind);
  374.     }
  375. }
  376.  
  377. //=============================================================================
  378. //    disable or enable our menus
  379. //=============================================================================
  380. void disableMenus(Boolean disable)
  381. {
  382.     if(disable) {
  383.         DisableItem(GetMHandle(mFile), 0);
  384.         DisableItem(GetMHandle(mDemo), 0);
  385.     }
  386.     else {
  387.         EnableItem(GetMHandle(mFile), 0);
  388.         EnableItem(GetMHandle(mDemo), 0);
  389.     }
  390.     DrawMenuBar();
  391. }
  392. //=============================================================================
  393. //    doMenuCommand
  394. //=============================================================================
  395. void doMenuCommand(long menuResult)
  396. {
  397.     short        menuID;                // The resource ID of the selected menu
  398.     short        menuItem;            // The item number of the selected menu
  399.     Str255        daName;
  400.     MenuHandle    menu;
  401.     
  402.     menuID = HIWORD(menuResult);
  403.     menuItem = LOWORD(menuResult);
  404.     switch ( menuID ) {
  405.         case mApple:
  406.             switch ( menuItem ) {
  407.                 case iAbout:
  408.                     (void) Alert(rAboutAlert, NIL);
  409.                 break;
  410.                 default:            // All non-About items in this menu are DAs
  411.                     GetItem(GetMHandle(mApple), menuItem, daName);
  412.                     (void) OpenDeskAcc(daName);
  413.                 break;
  414.             }
  415.         break;
  416.         case mFile:
  417.             switch ( menuItem ) {
  418.                 case iClose:
  419.                     closeAnyWindow(FrontWindow());
  420.                 break;
  421.                 case iQuit:
  422.                     gQuit = TRUE;
  423.                 break;
  424.             }
  425.         break;
  426.         case mEdit:
  427.             switch (menuItem) {
  428.                 // Call SystemEdit for DA editing & MultiFinder
  429.                 // since we don’t do any Editing
  430.                 case iUndo:
  431.                 case iCut:
  432.                 case iCopy:
  433.                 case iPaste:
  434.                 case iClear:
  435.                     (void) SystemEdit(menuItem-1);
  436.                 break;
  437.             }
  438.         break;
  439.         case mDemo:
  440.             switch (menuItem) {
  441.                 case iDemoWind:
  442.                     doNewWindow();                
  443.                 break;
  444.                 case iDemoDlog:
  445.                     HiliteMenu(0);
  446.                     disableMenus(true);        // shut off our menus
  447.                     
  448.                     demoDialog();
  449.                     
  450.                     disableMenus(false);    // bring our menus back
  451.                 break;
  452.                 case iComparePopup:
  453.                     HiliteMenu(0);
  454.                     disableMenus(true);        // shut off our menus
  455.                     
  456.                     comparePopup();
  457.                     
  458.                     disableMenus(false);    // bring our menus back
  459.                 break;
  460.                 case iDemoTab1:
  461.                 case iDemoTab2:
  462.                 case iDemoTab3:
  463.                 case iDemoTab4:
  464.                     HiliteMenu(0);
  465.                     disableMenus(true);        // shut off our menus
  466.                     
  467.                     demoTab(menuItem);
  468.                     
  469.                     disableMenus(false);    // bring our menus back
  470.                 break;
  471.             }
  472.         break;
  473.     }
  474.     HiliteMenu(0);        // Unhighlight what MenuSelect or MenuKey hilited
  475. }
  476.  
  477. //=============================================================================
  478. //    close a window, regardless of type
  479. //=============================================================================
  480. void closeAnyWindow(WindowPtr window)
  481. {
  482.     if (isDAWindow(window)) {
  483.         CloseDeskAcc( ( (WindowPeek) window )->windowKind );
  484.     } 
  485.     else if (isDialogWindow(window)) {
  486.         HideWindow(window);
  487.     } 
  488.     else if (isAppWindow(window)) {            // has to be our window
  489.         doCloseWindow(window);                                        
  490.     }
  491. }
  492.  
  493. //=============================================================================
  494.  
  495. //    doAlert
  496.  
  497. //=============================================================================
  498. void doAlert(short errNumber, Boolean exit)
  499. {
  500.     short        itemHit;
  501.     Str255        theMessage;
  502.  
  503.     SetCursor(&qd.arrow);
  504.     GetIndString(theMessage, rErrorStrings, errNumber);
  505.     ParamText(theMessage, NIL, NIL, NIL);
  506.     itemHit = StopAlert(rErrorAlert, NIL);
  507.     if(exit)
  508.         ExitToShell();
  509. }
  510.  
  511. //=============================================================================
  512.  
  513. //    isAppWindow
  514.  
  515. //=============================================================================
  516. Boolean isAppWindow(WindowPtr window)
  517. {
  518.     short        windowKind;
  519.  
  520.     if ( window == NIL )
  521.         return false;
  522.     else {
  523.         windowKind = ((WindowPeek) window)->windowKind;
  524.         return ((windowKind >= userKind) || (windowKind == dialogKind));
  525.     }
  526. }
  527.  
  528. //=============================================================================
  529.  
  530. //    isDAWindow
  531.  
  532. //=============================================================================
  533. Boolean isDAWindow(WindowPtr window)
  534. {
  535.     if ( window == NIL )
  536.         return false;
  537.     else
  538.         return ( ((WindowPeek) window)->windowKind < 0 );
  539. }
  540.  
  541. //=============================================================================
  542.  
  543. //    isDialogWindow
  544.     
  545. //=============================================================================
  546. Boolean isDialogWindow(WindowPtr window)
  547. {
  548.     if ( window == NIL )
  549.         return false;
  550.     else
  551.         return ( ((WindowPeek) window)->windowKind == dialogKind );
  552. }
  553.  
  554. //=============================================================================
  555.  
  556. //    invalidateScrollbars
  557.  
  558. //=============================================================================
  559. void invalidateScrollbars(WindowPtr theWindow)
  560. {
  561.     Rect    tempRect;
  562.  
  563.     SetPort(theWindow);
  564.  
  565.     tempRect = theWindow->portRect;
  566.     tempRect.left = tempRect.right - 15;
  567.     InvalRect(&tempRect);
  568.     EraseRect(&tempRect);
  569.  
  570.     tempRect = theWindow->portRect;
  571.     tempRect.top = tempRect.bottom - 15;
  572.     InvalRect(&tempRect);
  573.     EraseRect(&tempRect);
  574. }
  575.  
  576. //=============================================================================
  577.  
  578. //    doNewWindow
  579.  
  580. //=============================================================================
  581. WindowPtr    doNewWindow()
  582. {
  583.     WindowPtr    theWind;
  584.  
  585.     if(gSysEnv.hasColorQD)
  586.         theWind = GetNewCWindow(kNewWindowID, NIL, (WindowPtr) -1);
  587.     else
  588.         theWind = GetNewWindow(kNewWindowID, NIL, (WindowPtr) -1);
  589.         
  590.     if (theWind != NIL) {
  591.         SetPort(theWind);
  592.         TextFont(geneva);
  593.         TextSize(9);
  594.         
  595. // Create some controls in the new window
  596.  
  597.         doNewControls(theWind);    
  598.         make3DControls(theWind);
  599.         gDemoType = 1;
  600.     
  601.         ShowWindow(theWind);
  602.         return(theWind);
  603.     }
  604.     return(0L);
  605. }
  606.  
  607.  
  608.  
  609. //=============================================================================
  610. //    Close our window, since from a resource, use DisposeWindow
  611. //=============================================================================
  612. void    doCloseWindow(WindowPtr theWind)
  613. {
  614.     DisposeWindow(theWind);
  615.     gDemoType = 0;
  616. }